package com.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dao.dao.IGoodsDao;
import com.dao.dao.IOrderDao;
import com.dao.dao.IOrderGoodsDao;
import com.dao.entity.Goods;
import com.dao.entity.Order;
import com.dao.entity.OrderGoods;
import com.dao.redis.IRedislLoginVerifyDao;
import com.net.base.BaseRequest;
import com.net.base.BaseResponse;
import com.net.bean.OrderResultBean;
import com.net.filter.LoginFilter;
import com.util.NetUtil;
import com.util.Utils;

/**
 * 
 * 订单处理控制器
 * 
 * @author 尚业
 *
 */
@Controller
@RequestMapping(value = "/order")
public class OrderControl {
	private static Logger logger = Logger.getLogger(LoginFilter.class);
	
	@Autowired
	IGoodsDao goodsDao;
	@Autowired
	IOrderDao orderDao;
	@Autowired
	IOrderGoodsDao orderGoodDao;
	@Autowired
	IRedislLoginVerifyDao redislLoginVerifyDao;
	/**
	 * 1.获取事物控制器
	 */
	@Autowired
	DataSourceTransactionManager transactionManager;
	
	/**
	 * 修改订单状态
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/changeOrderStatus")
	@ResponseBody
	public BaseResponse<Integer> sendGoods(@RequestBody BaseRequest<Map<String, Integer>> request){
		if (request == null) {
			return NetUtil.getCommonFailuerResponse("请求数据为空");
		}
		Map<String, Integer> orderValue = request.getBody();
		Order order=orderDao.selectByPrimaryKey(orderValue.get("id"));
		order.setState(orderValue.get("status"));
		return NetUtil.returnResponseByDbCode(orderDao.updateByPrimaryKey(order));
	}
	/**
	 * 根据订单状态查询订单
	 */
	@RequestMapping(value = "/queryOrderByState")
	@ResponseBody
	public BaseResponse<List<OrderResultBean>> queryOrderByState(HttpServletRequest request,
			@RequestBody BaseRequest<Map<String, String>> requestBody) {
		if (requestBody == null) {
			return NetUtil.getCommonFailuerResponse("请求数据为空");
		}
		Map<String, String> orderInfo = requestBody.getBody();
		if (orderInfo == null) {
			return NetUtil.getCommonFailuerResponse("请求数据为空");
		}
		String name1 = Utils.getName1Bytaken(redislLoginVerifyDao,
				request.getHeader("JSESSIONID"));
		String shopname = Utils.getShopnameBytaken(redislLoginVerifyDao,
				request.getHeader("JSESSIONID"));
		if (name1 == null && shopname == null) {
			return NetUtil.getCommonFailuerResponse("会话过期");
		}
		String sellerName = orderInfo.get("seller");
		List<OrderResultBean> resultBeans = new ArrayList<OrderResultBean>();
		// 2.获取事物定义
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
		// 3.设置事务隔离级别，开启新事务
		def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
		// 4.获得事务状态
		TransactionStatus status = transactionManager.getTransaction(def);
		try {
			List<Order> listResult = null;
			Integer statusInteger = Integer.parseInt(orderInfo.get("state"));
			
			if(sellerName!=null){
				if(statusInteger==4){
					listResult = orderDao.queryAllOrderByStateSeller(shopname);
				}else{
					listResult = orderDao.queryOrderByStateSeller(shopname, statusInteger);
				}	
			}else{
				if(statusInteger==4){
					listResult = orderDao.queryAllOrderByState(name1);
				}else {
					listResult = orderDao.queryOrderByState(name1,statusInteger);
				}
			}
			for(int i=0;i<listResult.size();i++){
				List<OrderGoods> orderGoodsList = orderGoodDao.queryByOrderId(listResult.get(i).getOrderid());
				List<String> images = new ArrayList<String>();
				for(int j=0;j<orderGoodsList.size();j++){
					Goods goods = goodsDao.selectByPrimaryKey(orderGoodsList.get(j).getGoodsid());
					images.add(goods.getImage());
				}
				resultBeans.add(new OrderResultBean(listResult.get(i),orderGoodsList,images));
			}
					
			transactionManager.commit(status);
					
		} catch (Exception e) {
			transactionManager.rollback(status);
			logger.error("查询订单错误", e);
			//return NetUtil.getCommonFailuerResponse("查询订单");
		}
		
		return NetUtil.getDataResponse(resultBeans, "数据请求成功");
	}
	/**
	 * 根据订单状态查询订单数量
	 */
	@RequestMapping(value = "/queryOrderCountByState")
	@ResponseBody
	public BaseResponse<Integer> queryOrderCountByState(HttpServletRequest request,
			@RequestBody BaseRequest<Map<String, Integer>> requestBody) {
		if (requestBody == null) {
			return NetUtil.getCommonFailuerResponse("请求数据为空");
		}
		Map<String, Integer> orderId = requestBody.getBody();
		if (orderId == null) {
			return NetUtil.getCommonFailuerResponse("请求数据为空");
		}
		String name1 = Utils.getName1Bytaken(redislLoginVerifyDao,
				request.getHeader("JSESSIONID"));
		int count = orderDao.queryOrderCountByState(orderId.get("state"),name1);
		return NetUtil.getDataResponse(count, "数据请求成功");
	}
}
